package com.originskyseed.platform.user.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.user.mapper.ComplianceInspectionRulesMapper;
import com.originskyseed.platform.user.model.ComplianceInspectionRules;
import com.originskyseed.platform.user.service.IComplianceInspectionRulesService;

/**
 * 合规检查扣分规则表Service接口实现类（OSI_COMPLIANCE_INSPECTION_RULES）
 * 
 * @author Tangwanghui
 */
@DubboService
@Component("complianceInspectionRulesService")
public class ComplianceInspectionRulesServiceImpl implements IComplianceInspectionRulesService {

	@Resource
	private ComplianceInspectionRulesMapper complianceInspectionRulesMapper;

	/**
	 * 删除记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.complianceInspectionRulesMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 * 
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.complianceInspectionRulesMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 插入记录
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(ComplianceInspectionRules record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.complianceInspectionRulesMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(ComplianceInspectionRules record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.complianceInspectionRulesMapper.insertSelective(record);
	}

	/**
	 * 更新记录
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(ComplianceInspectionRules record) throws Exception {
		record.setUpdateTime(new Date());
		return this.complianceInspectionRulesMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(ComplianceInspectionRules record) throws Exception {
		record.setUpdateTime(new Date());
		return this.complianceInspectionRulesMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public ComplianceInspectionRules selectByPrimaryKey(String id) throws Exception {
		return this.complianceInspectionRulesMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询列表
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<ComplianceInspectionRules> selectList(ComplianceInspectionRules record) throws Exception {
		return this.complianceInspectionRulesMapper.selectList(record);
	}

	/**
	 * 分页查询列表
	 * 
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<ComplianceInspectionRules> selectListByPage(Map<String, Object> params, BasePage<ComplianceInspectionRules> page) throws Exception {
		params.put("page", page);
		List<ComplianceInspectionRules> list = this.complianceInspectionRulesMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 置顶接口
	 *
	 * @param complianceInspectionRules
	 * @return
	 * @throws Exception
	 */
	public int updateIsTop(ComplianceInspectionRules complianceInspectionRules) throws Exception {
		return this.complianceInspectionRulesMapper.updateIsTop(complianceInspectionRules);
	}

	/**
	 * 查询最大的优先级
	 * 
	 * @return
	 * @throws Exception
	 */
	public Integer selectPriority() throws Exception {
		return this.complianceInspectionRulesMapper.selectPriority();
	}

	/**
	 * 根据优先级查询
	 * 
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public ComplianceInspectionRules selectComplianceByPrioritySort(Map<String, Object> map) throws Exception {
		return this.complianceInspectionRulesMapper.selectComplianceByPrioritySort(map);
	}

	/**
	 * 更新排序信息
	 *
	 * @param downCompliance
	 * @param upCompliance
	 * @return
	 * @throws Exception
	 */
	public int updatePrority(ComplianceInspectionRules downCompliance, ComplianceInspectionRules upCompliance) throws Exception {
		downCompliance.setUpdateTime(new Date());
		upCompliance.setUpdateTime(new Date());
		this.complianceInspectionRulesMapper.updateByPrimaryKeySelective(downCompliance);
		return this.complianceInspectionRulesMapper.updateByPrimaryKeySelective(upCompliance);
	}
}